<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="检查类型名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入检查类型名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="检查类型值" prop="value">
        <el-input
          v-model="queryParams.value"
          placeholder="请输入检查类型值"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="允许准备中人数" prop="readyNum">
        <el-input
          v-model="queryParams.readyNum"
          placeholder="请输入允许准备中人数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="叫号列" prop="callingColumn">
        <el-input
          v-model="queryParams.callingColumn"
          placeholder="请输入叫号列"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否显示患者条码" prop="displayBarcode">
        <el-input
          v-model="queryParams.displayBarcode"
          placeholder="请输入是否显示患者条码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否需要确费" prop="expenseRecognition">
        <el-input
          v-model="queryParams.expenseRecognition"
          placeholder="请输入是否需要确费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="时段预约规定人数" prop="timeslotBookNum">
        <el-input
          v-model="queryParams.timeslotBookNum"
          placeholder="请输入时段预约规定人数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="每时段预留人数" prop="timeslotReservedNum">
        <el-input
          v-model="queryParams.timeslotReservedNum"
          placeholder="请输入每时段预留人数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="亲和的检查类型" prop="affinityCheckTypes">
        <el-input
          v-model="queryParams.affinityCheckTypes"
          placeholder="请输入亲和的检查类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ecg:check-type:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['ecg:check-type:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="检查类型名" align="center" prop="name" width="180px"/>
      <el-table-column label="检查类型值" align="center" prop="value" />
      <el-table-column label="允许准备中人数" align="center" prop="readyNum" />
<!--      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px />-->
      <el-table-column label="需要设备领用" align="center" prop="needDevReady">
        <template #default="scope">
          {{scope.row.needDevReady === 0 ? "不需要":"需要"}}
        </template>
      </el-table-column>
      <el-table-column label="叫号列" align="center" prop="callingColumn" />
      <el-table-column label="是否显示患者条码" align="center" prop="displayBarcode" />
      <el-table-column label="是否需要确费" align="center" prop="expenseRecognition" />
      <el-table-column label="时段预约规定人数" align="center" prop="timeslotBookNum" />
      <el-table-column label="每时段预留人数" align="center" prop="timeslotReservedNum" />
      <el-table-column label="预约注意事项" align="center" prop="notes" />
      <el-table-column label="序号前缀" align="center" prop="seqPrefix" />
      <el-table-column label="亲和的检查类型" align="center" prop="affinityCheckTypes" >
        <template #default="scope">
          <div v-for="(checkType, subIndex) in scope.row.affinityCheckTypes" :key="subIndex">
            {{checkTypeStore.getCheckTypeName(checkType)}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['ecg:check-type:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['ecg:check-type:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CheckTypeForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import download from '@/utils/download'
import { CheckTypeApi, CheckTypeVO } from '@/api/ecg/checktype'
import CheckTypeForm from './CheckTypeForm.vue'
import {useCheckTypeStore} from "@/store/modules/checkType";

/** 检查类型 列表 */
defineOptions({ name: 'CheckType' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const checkTypeStore = useCheckTypeStore();

const loading = ref(true) // 列表的加载中
const list = ref<CheckTypeVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  value: undefined,
  readyNum: undefined,
  createTime: [],
  needDevReady: undefined,
  callingColumn: undefined,
  displayBarcode: undefined,
  expenseRecognition: undefined,
  timeslotBookNum: undefined,
  timeslotReservedNum: undefined,
  affinityCheckTypes: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CheckTypeApi.getCheckTypePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CheckTypeApi.deleteCheckType(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CheckTypeApi.exportCheckType(queryParams)
    download.excel(data, '检查类型.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>